﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace LuKou.Web.Core
{
    public class Security
    {
        public static byte[] DESDecrypt(byte[] encryptedValue, string key)
        {
            return DESDecrypt(encryptedValue, key, key);
        }

        public static string DESDecrypt(string encryptedValue, string key)
        {
            return DESDecrypt(encryptedValue, key, key);
        }

        public static byte[] DESDecrypt(byte[] encryptedValue, string key, string IV)
        {
            key = key + "abcdefgh";
            IV = IV + "01234567";
            key = key.Substring(0, 8);
            IV = IV.Substring(0, 8);
            ICryptoTransform transform = new DESCryptoServiceProvider { Key = Encoding.UTF8.GetBytes(key), IV = Encoding.UTF8.GetBytes(IV) }.CreateDecryptor();
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write);
            stream2.Write(encryptedValue, 0, encryptedValue.Length);
            stream2.FlushFinalBlock();
            stream2.Close();
            return stream.ToArray();
        }

        public static string DESDecrypt(string encryptedValue, string key, string IV)
        {
            key = key + "abcdefgh";
            IV = IV + "01234567";
            key = key.Substring(0, 8);
            IV = IV.Substring(0, 8);
            ICryptoTransform transform = new DESCryptoServiceProvider { Key = Encoding.UTF8.GetBytes(key), IV = Encoding.UTF8.GetBytes(IV) }.CreateDecryptor();
            byte[] buffer = Convert.FromBase64String(encryptedValue);
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write);
            stream2.Write(buffer, 0, buffer.Length);
            stream2.FlushFinalBlock();
            stream2.Close();
            return Encoding.UTF8.GetString(stream.ToArray());
        }

        public static string DESEncrypt(string originalValue, string key)
        {
            return DESEncrypt(originalValue, key, key);
        }

        public static byte[] DESEncrypt(byte[] originalValue, string key)
        {
            return DESEncrypt(originalValue, key, key);
        }

        public static byte[] DESEncrypt(byte[] originalValue, string key, string IV)
        {
            key = key + "abcdefgh";
            IV = IV + "01234567";
            key = key.Substring(0, 8);
            IV = IV.Substring(0, 8);
            ICryptoTransform transform = new DESCryptoServiceProvider { Key = Encoding.UTF8.GetBytes(key), IV = Encoding.UTF8.GetBytes(IV) }.CreateEncryptor();
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write);
            stream2.Write(originalValue, 0, originalValue.Length);
            stream2.FlushFinalBlock();
            stream2.Close();
            return stream.ToArray();
        }

        public static string DESEncrypt(string originalValue, string key, string IV)
        {
            if (string.IsNullOrEmpty(originalValue))
            {
                originalValue = string.Empty;
            }
            key = key + "abcdefgh";
            IV = IV + "01234567";
            key = key.Substring(0, 8);
            IV = IV.Substring(0, 8);
            ICryptoTransform transform = new DESCryptoServiceProvider { Key = Encoding.UTF8.GetBytes(key), IV = Encoding.UTF8.GetBytes(IV) }.CreateEncryptor();
            byte[] bytes = Encoding.UTF8.GetBytes(originalValue);
            MemoryStream stream = new MemoryStream();
            CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Write);
            stream2.Write(bytes, 0, bytes.Length);
            stream2.FlushFinalBlock();
            stream2.Close();
            return Convert.ToBase64String(stream.ToArray());
        }
    }
}
